﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM

using System.Collections.Generic;
using YunSpace.Core;
using YunSpace.Data;
using YunSpace.Domain.Entity.Topic;
using YunSpace.Domain.IRepository.Topic;
using YunSpace.Domain.ViewModel.WorkFlow;
using YunSpace.Domain.ViewModel.Topic;

#endregion

namespace YunSpace.Repository.Topic
{
    public class ArticleRepository : RepositoryBase<ArticleEntity>, IArticleRepository
    {
        public List<ArticleDto> GetArticles(GetGridJsonRequest request, Pagination pagination)
        {
            var sqlStr = @"
                    SELECT  F_Id AS Id,
                            F_Types AS Type,
                            F_Class AS Class,
                            F_Source AS Source,
                            left(F_Title, 100) AS Title,
                            F_Time AS Time,
                            F_HomeRecommenda AS IsHomeRecommend,
                            F_CreationPeople ,
                            F_State AS State,
                            F_Hot AS IsHot,
                            F_HotRecTime ,
                            F_RecCity AS RecommendCity,
                            F_IsSpecial AS IsSpecial,
                            F_FirstPicture
                    FROM    Topic_Article AS a
                    WHERE 1=1 ";

            var filterSql = "";

            if (!request.F_Title.IsNullOrEmpty())
            {
                filterSql += $" AND a.F_Title LIKE '%{request.F_Title}%'";
            }

            if (!request.F_Types.IsNullOrEmpty())
            {
                filterSql += $" AND a.F_Types = '{request.F_Types}'";
            }

            if (!request.F_Source.IsNullOrEmpty())
            {
                filterSql += $" AND a.F_Source = '{request.F_Source}'";
            }

            if (!request.F_State.IsNullOrEmpty())
            {
                filterSql += $" AND a.F_State = '{request.F_Title}'";
            }

            if (!request.F_RecCity.IsNullOrEmpty())
            {
                filterSql += $" AND a.F_RecCity LIKE '%{request.F_RecCity}%'";
            }

            if (!request.F_Class.IsNullOrEmpty())
            {
                filterSql += $" AND a.F_Class LIKE '%{request.F_Class}%'";
            }

            pagination.records = GetArticlesCount(filterSql);

            var startNo = pagination.rows * (pagination.page - 1);
            var endNo = startNo + pagination.rows;

            sqlStr = $"{sqlStr} {filterSql} ORDER BY {pagination.sidx} LIMIT {startNo},{endNo};";

            using (var db = new RepositoryBase())
            {
                var result = db.FindList<ArticleDto>(sqlStr);
                return result;
            }
        }

        private int GetArticlesCount(string filterSql)
        {
            var sql = $@"SELECT  COUNT(1)
                                FROM    Topic_Article AS a
                                WHERE 1 = 1 {filterSql};";

            return Count(sql);
        }
    }
}

